import Vue from 'vue'
import Router from 'vue-router'
import tabBar from './tabBar.js'
//下面四句代码是为了处理router.beforeEach的异常报错(https://blog.csdn.net/haidong55/article/details/100939076)
const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch((err) => err)
}

Vue.use(Router)

/**
 * meta属性
 *     title:标题
 *     auth:是否需要登录
 *     keepAliv:是否缓存
 *     showTab:是否展示底部tab导航
 */

const router = new Router({
  mode: 'hash',
  base: '/',
  routes: [
    ...tabBar, //tabBar导航
    {
      path: '/',
      redirect: '/home',
    },
    {
      path: '/index',
      component: () => import('@/pages/home/index.vue'),
      meta: {
        title: '首页',
        auth: false,
      },
    },
    {
      path: '/first-three',
      component: () => import('@/pages/three-page/first-three.vue'),
      meta: {
        title: '第一个页面',
        auth: false,
      },
    },
    {
      path: '/draw-line',
      component: () => import('@/pages/three-page/draw-line.vue'),
      meta: {
        title: '画线',
        auth: false,
      },
    },
    {
      path: '/creat-text',
      component: () => import('@/pages/three-page/creat-text.vue'),
      meta: {
        title: '创建文字',
        auth: false,
      },
    },
    {
      path: '/loading-3d-moidel',
      component: () => import('@/pages/three-page/loading-3d-moidel.vue'),
      meta: {
        title: '载入3D模型',
      },
    },
    {
      path: '/sceneBasic',
      component: () => import('@/pages/three-page/sceneBasic.vue'),
      meta: {
        title: '测试场景类',
        auth: false,
      },
    },
    {
      path: '/control-cube',
      component: () =>
        import('@/pages/three-page/control-cube/control-cube.vue'),
      meta: {
        title: '控制立方体',
        auth: false,
      },
    },
    {
      path: '/cude-animation',
      component: () =>
        import('@/pages/three-page/control-cube/cude-animation.vue'),
      meta: {
        title: 'gsap动画',
        auth: false,
      },
    },
    {
      path: '/cude-tools',
      component: () => import('@/pages/three-page/control-cube/cude-tools.vue'),
      meta: {
        title: 'cube-tool工具',
        auth: false,
      },
    },
    {
      path: '/cube',
      component: () => import('@/pages/three-page/cube-learning/cube.vue'),
      meta: {
        title: 'cube学习',
      },
    },
    {
      path: '/spherome',
      component: () => import('@/pages/three-page/spherome.vue'),
      meta: {
        title: '球体',
        auth: false,
      },
    },
    {
      path: '/material',
      component: () => import('@/pages/three-page/material/material.vue'),
      meta: {
        title: '材质',
        auth: false,
      },
    },
    {
      path: '/spherome-vr',
      component: () => import('@/pages/three-page/spherome-vr/index.vue'),
      meta: {
        title: '球体-vr',
        auth: false,
      },
    },
    {
      path: '/house',
      component: () => import('@/pages/three-page/house/house.vue'),
      meta: {
        title: '房子',
        auth: false,
      },
    },
    {
      path: '/three-game-hand',
      component: () =>
        import('@/pages/three-page/three-game/three-game-hand.vue'),
      meta: {
        title: 'three.js游戏-手势',
        auth: false,
      },
    },
    {
      path: '/three-game-body',
      component: () =>
        import('@/pages/three-page/three-game/three-game-body.vue'),
      meta: {
        title: 'three.js游戏-身体',
        auth: false,
      },
    },
  ],
})

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})
export default router
